package com.kyzh.core.activities

import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.gushenge.atools.util.AView
import com.gushenge.core.base.Base
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Task
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.MMKVConsts.uid
import com.gushenge.core.impls.WealImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.pager.weal.sign.SignActivity
import com.kyzh.core.adapters.TaskCenterAdapter
import com.kyzh.core.pager.weal.share.ShareNewActivity
import com.kyzh.core.utils.*
import kotlinx.android.synthetic.main.activity_taskcenter.*
import org.jetbrains.anko.toast

class TaskCenterActivity : BaseActivity(),ResultListener {

    val context = this@TaskCenterActivity
    val bean = arrayListOf<Task>()
    val adapter= TaskCenterAdapter(R.layout.simple_list_item_4,bean)
    var load: AlertDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_taskcenter)
        initView()
        initData()
    }

    private fun initData() {

        recyclerView.layoutManager = object :LinearLayoutManager(context){
            override fun canScrollHorizontally(): Boolean {
                return false
            }
        }
        recyclerView.adapter = adapter
    }

    private fun initView() {
        close.setOnClickListener { finish() }
        AView.setStatusBar(context,false)
        AView.setHeight(rlBg,AView.getStatusBarHeight(context)+dip(145))
        load = loading()
        scoreMarket.setOnClickListener { startActivity<PointsMallActivity>() }
        ivTitleMore.setOnClickListener { startActivity<PointDetailActivity>() }
        pointCJ.setOnClickListener {
            startActivity<BrowserActivity>(GlobalKeys.TITLE to getString(R.string.pointLottery),GlobalKeys.LINK to "${Base.BASEURL}/?ct=choujiang&uid=$uid&t=${time}&sign=${httpSign(uid)}")
        }
    }

    override fun success(beans: Any, message: String) {
        beans as ArrayList<Task>
        bean.clear()
        tvPoint.text = message
        bean.addAll(beans)
        adapter.notifyDataSetChanged()
        load?.dismiss()
        adapter.setOnItemClickListener { adapter, view, position ->
            if (beans[position].url.isNotEmpty()){
                startActivity<BrowserActivity>(GlobalKeys.TITLE to beans[position].name, GlobalKeys.LINK to beans[position].url)
            }else{
                when(beans[position].type){
                    0->startActivity<SignActivity>()
                    2->startActivity<TodayTaskActivity>()
                    1->startActivity<NewTaskActivity>()
                    4->startActivity<ShareNewActivity>()
                    else->toast(getString(R.string.functionNotOpen))
                }
            }
        }
    }

    override fun error(error: String) {
        toast(error)
        load?.dismiss()
    }

    override fun onResume() {
        super.onResume()
        WealImpl.getTasks(context)
    }
}
